package com.yunhe.merchants.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ModelNotFoundException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.listener.UploadDataListener;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.merchants.constant.MyProjectConsts;
import com.yunhe.merchants.domain.*;
import com.yunhe.common.utils.ExcelUtils;
import com.yunhe.merchants.domain.MyProject;
import com.yunhe.merchants.domain.ProcessStatus;
import com.yunhe.merchants.model.FilterCondition;
import com.yunhe.merchants.model.IntentionProjectUpload;
import com.yunhe.merchants.service.*;
import com.yunhe.merchants.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: DongWenYu
 * @Date: 2021/5/7 22:06
 */
@Api(value = "intention", description = "意向项目", tags = {"Model_Intention_Project"})
@RestController
@RequestMapping("/intention")
public class IntentionProjectController {

    @Autowired
    IIntentionProjectService intentionProjectService;

    @Autowired
    IProcessStatusService processStatusService;

    @Autowired
    IHistoryProjectService historyProjectService;

    @Autowired
    IDataHistoryProjectStatusService dataHistoryProjectStatusService;

    @Resource(name = "myProjectServiceImpl")
    IMyProjectService myProjectService;

    @Autowired
    MyHeaderUtil headerUtil;

    @ApiOperation("意向项目条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping
    public ResponseEntity<ResultObject> selectByCondition(@ModelAttribute FilterCondition filterCondition,
                                                                   @RequestParam("page") int page,
                                                                   @RequestParam("size") int size) throws ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null ){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        PageResult result = intentionProjectService.selectByFilterCondition(filterCondition,userId,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("新增项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "园区id", dataType = "long", paramType = "query"),
    })
    @PostMapping
    public ResponseEntity<ResultObject> save(@RequestBody MyProject myProject ,
                                             @RequestParam("stationId") Long stationId) throws ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        myProjectService.insert(myProject , userId ,stationId);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("数据导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "园区id", dataType = "long", paramType = "query"),
    })
    @PostMapping("/upload")
    public ResponseEntity<ResultObject> upload(@RequestBody MultipartFile file,
                                               @RequestParam Long stationId) throws IOException, ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        EasyExcel.read(file.getInputStream(), IntentionProjectUpload.class,
                new UploadDataListener(intentionProjectService,file.getOriginalFilename(),userId,stationId)).sheet().doRead();
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("数据导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "意向项目的id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/export")
    public ResponseEntity<ResultObject> export(@RequestParam("ids") String ids,
                                               @ModelAttribute FilterCondition filterCondition,
                                               HttpServletResponse httpServletResponse) {
        List<MyProject> data = intentionProjectService.export(ids , filterCondition);
        ExcelUtils.convertAndExport(data , httpServletResponse , IntentionProjectUpload.class);
        return null;
    }

    @ApiOperation("查看意向项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "意向项目id", dataType = "long", paramType = "query")
    })
    @GetMapping("/detail")
    public ResponseEntity<ResultObject> selectById(@RequestParam("id") Long id){
        MyProject result = intentionProjectService.selectById(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("查看意向项目的历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "意向项目id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping("/history")
    public ResponseEntity<ResultObject> selectHistory(@RequestParam("id") Long id,
                                                               @RequestParam("page") int page,
                                                               @RequestParam("size") int size){
        PageResult result = dataHistoryProjectStatusService.selectByProjectId(id,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("查看意向项目的状态流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "状态流程id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query")
    })
    @GetMapping("/status")
    public ResponseEntity<ResultObject> selectStatus(@RequestParam("id") Long id,
                                                               @RequestParam(value = "status" , required = false) String status){
        ProcessStatus result = intentionProjectService.selectStatusById(id,status);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("更新意向项目的状态流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "意向项目id", dataType = "long", paramType = "query")
    })
    @PostMapping("/status")
    public ResponseEntity<ResultObject> updateProcessStatus(@RequestBody ProcessStatus processStatus,
                                                            @RequestParam("id") Long id) throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException, ModelNotFoundException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        processStatusService.update(processStatus , id , userId);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }


    @ApiOperation("项目中心-流程管理-查看项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "一页展示数量", dataType = "int", paramType = "query")
    })
    @PostMapping("/searchProjectStatusList")
    public ResponseEntity<ResultObject> searchProjectStatusList(@RequestBody FilterCondition filterCondition,
                                                                @ModelAttribute PageParam pageParam)
            throws ArgumentErrorException {
        PageResult result = myProjectService.selectForLatestStatusList(filterCondition, pageParam.getPage(), pageParam.getSize());
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("项目中心-离场项目-查看项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "一页展示数量", dataType = "int", paramType = "query")
    })
    @PostMapping("/searchLeaveTheFieldList")
    public ResponseEntity<ResultObject> searchLeaveTheFieldList(@RequestBody FilterCondition filterCondition,
                                                                @ModelAttribute PageParam pageParam)
            throws ArgumentErrorException {
        filterCondition.setCompanyStatus(MyProjectConsts.COMPANY_STATUS.Leave.value());
        filterCondition.setProjectStatus(MyProjectConsts.COMPANY_STATUS.Leave.value());
        PageResult result = myProjectService.selectForLatestStatusList(filterCondition, pageParam.getPage(), pageParam.getSize());
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }
}
